package com.qf.controller;

import com.alibaba.fastjson.JSON;
import com.qf.pojo.Catalog;
import com.qf.service.CatalogService;
import com.qf.service.impl.CatalogServiceImpl;
import com.qf.vo.CatalogVO;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2021/10/19.
 */
@WebServlet("/catalog")
public class CatalogController extends BaseController {
    private CatalogService catalogService=new CatalogServiceImpl();

    public String byLevel1(HttpServletRequest request, HttpServletResponse response){

        //接收参数
        String line=request.getParameter("line");
        int lines=Integer.valueOf(line);
        //调用业务
        List<Catalog> catalogList = catalogService.selectCatalogByLevel2(lines);


        List<CatalogVO> catalogVOS=new ArrayList<>();//c2/c3s
        //遍历所有二级类别
//        for(int i=0;i<catalogList.size();i++){
        for(Catalog catalog:catalogList){
            //绿框框
            CatalogVO vo=new CatalogVO();//给每个创建的对象属性赋值
            //c2属性：红框框
            vo.setC2(catalog);
            //根据二级类别的id查询对应所有三级类别
            //c3s属性：蓝框框
            vo.setC3s(catalogService.selectCatalogByLevel3(catalog.getCid()));
            catalogVOS.add(vo);
        }
        //List<vo对象>---Catalog/List<Catalog>
        return JSON.toJSONString(catalogVOS);
        //跳转
    }
}
